/****************************************************************************
 * Copyright 2009 Huascar A. Sanchez                                        *
 *                                                                          *
 * Licensed under the Apache License, Version 2.0 (the "License");          *
 * you may not use this file except in compliance with the License.         *
 * You may obtain a copy of the License at                                  *
 *                                                                          *
 *     http://www.apache.org/licenses/LICENSE-2.0                           *
 *                                                                          *
 * Unless required by applicable law or agreed to in writing, software      *
 * distributed under the License is distributed on an "AS IS" BASIS,        *
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. *
 * See the License for the specific language governing permissions and      *
 * limitations under the License.                                           *
 ****************************************************************************/
package com.gotobject.filter.common;

import static com.gotobject.filter.common.Aggregations.count;
import static com.gotobject.filter.common.Predicates.*;
import static com.gotobject.filter.common.Initializers.range;
import static com.gotobject.filter.common.Initializers.repeat;
import static com.gotobject.filter.common.Iterables.asArray;
import org.junit.Assert;
import org.junit.Test;

/**
 * @author huascar.sanchez@gmail.com (Huascar A. Sanchez)
 */
public class InitializersTest {
    @Test
    public void testRangeInitializers_NoConditions() throws Exception {
        final Iterable<Integer> r = range(0, 10);
        Assert.assertArrayEquals(
                "same content-size",
                asArray(r),
                new Integer[]{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}
        );

        final Iterable<Integer> r2 = range(4, 12);
        Assert.assertArrayEquals(
                "same content-size",
                asArray(r2),
                new Integer[]{4, 5, 6, 7, 8, 9, 10, 11}
        );
    }

    @Test
    public void testRangeInitializers_NoConditions_Characters() throws Exception {
        final Iterable<Character> c = range('A', 'F');
        Assert.assertArrayEquals(
                "same content-size",
                asArray(c),
                new Character[]{'A', 'B', 'C', 'D', 'E', 'F'}
        );

    }


    @Test
    public void testRangeInitializers_WithConditions_Characters() throws Exception {
        final Iterable<Character> c = range('A', 'F', not(eq('C')));
        Assert.assertArrayEquals(
                "same content-size",
                asArray(c),
                new Character[]{'A', 'B', 'D', 'E', 'F'}
        );

    }


    @Test
    public void testRangeInitializers_WithConditions() throws Exception {
        final Iterable<Integer> r = range(0, 20, mod(3).and(not(eq(6).or(eq(9)))));
        Assert.assertArrayEquals(
                "same content-size",
                asArray(r),
                new Integer[]{0, 3, 12, 15, 18}
        );
    }

    @Test
    public void testRepeatInitializers() throws Exception {
        final Iterable<Integer> t = repeat(-1, 30);
        Assert.assertSame("30 instances of -1", 30, count(t, eq(-1)));
        final Iterable<String> x = repeat("A", 30);
        Assert.assertSame("30 instances of A", 30, count(x, eq("A")));                
    }
}
